<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/18
 * Time: 10:15
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\business\Auth;
use app\common\business\Know;
use app\common\business\Money;
use app\common\business\MoneyList;
use app\common\business\Wechat;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;
use think\facade\Log;

class User extends Base
{
    /**
     * @return \think\response\Json
     */
    public function getUserInfo()
    {
        $param = $this->request->param();
        $data = $param['userInfo'];
        $data['openid'] = $param['openid'];
        try {
            $wechat = (new Wechat())->WechatByOpenid($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "获取失败");
        }
        return Show::json_show(Status::mini_normal, "获取用户信息成功", $wechat);
    }

    /**
     * 获取是否认证
     * @return \think\response\Json
     * @throws Exception
     */
    public function getAuth()
    {
        $openid = $this->request->param('openid', '', 'trim');
        try {
            $user = (new Auth())->getAuthByOpenid($openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        //查询下单须知内容
        $mark = (new Know())->getBaseByFirstList();
        //获取用户信息
        $wechat = (new Wechat())->getOpenidById($openid);

        if ($user) {
            if ($user['state'] == 0) {
                $result_data = [
                    "is_edit" => 0,
                    "auth" => 0
                ];
            } else if ($user['state'] == 1) {
                $result_data = [
                    "is_edit" => 0,
                    "auth" => 1
                ];
            } else if($user['state'] == 3){
                $result_data = [
                    "is_edit" => 1,
                    "auth" => 2
                ];
            }else {
                $result_data = [
                    "is_edit" => 1,
                    "auth" => 0
                ];
            }
            $result_data['data'] = $user->toArray();
            $result_data['wechat'] = $wechat;
            $result_data['mark'] = $mark['mark'] ?? "";
            $result_data['mark'] = $mark['mark'] ?? "";
            return Show::json_show(Status::mini_normal, "ok", $result_data);
        } else {
            return Show::json_show(Status::mini_normal, "没有信息", [
                "auth" => 0,
                "is_edit" => 1,
                "mark"=>$mark['mark'] ?? "",
            ]);
        }
    }

    public function setUserKnow()
    {
        $openid = $this->request->param('openid', '', 'trim');
        if (!$openid) {
            return Show::json_show(Status::mini_error, "内部错误");
        }
        try {
            $openid = (new Wechat())->getOpenidById($openid);
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        if ($openid) {
            $result = $openid->save([
                "is_know" => 1
            ]);
            if ($result) {
                return Show::json_show(Status::mini_normal, "操作成功");
            } else {
                return Show::json_show(Status::mini_error, "操作失败");
            }
        } else {
            return Show::json_show(Status::mini_error, "操作失败");
        }
    }

    /**
     * 获取可提现金额
     * @return \think\response\Json
     */
    public function getUserMoney()
    {
        $openid = $this->request->param('openid', '', 'trim');
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "获取失败");
        }
        try {
            $money = (new Money())->getUserOpenidByMoneyCount($openid);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "内部错误");
        }
        return Show::json_show(Status::mini_normal, 'ok', [
            "money" => $money
        ]);
    }

    public function setMoneyOpenid()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $money = $this->request->param('money', '', 'trim');
        $relname = $this->request->param('relname', '', 'trim');
        $reCode = $this->request->param('reCode', '', 'trim');
        $data = [
            "openid" => $openid,
            "money" => $money,
            "relname" => $relname,
            "reCode" => $reCode
        ];
        if (floatval($money) < 5) {
            return Show::json_show(Status::mini_error, "金额必须大于20");
        }
        try {
            $res = (new MoneyList())->setMoneyOpenid($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "申请成功");
        } else {
            return Show::json_show(Status::mini_normal, "申请失败");
        }
    }

    public function getMoneyListByList()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $limit = $this->request->param('limit', 10, 'intval');
        $page = $this->request->param('page', 1, 'intval');
        try {
            $lists = (new MoneyList())->getMoneyListByList([
                ["openid", '=', $openid]
            ], $limit, $page);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }
}